﻿using UnityEditor;
using UnityEngine;

class Calculator : EditorWindow
{
    int mFirstNumber;
    string mSymbol;
    int mSecondNumber;

    public int Result;

    public void InputFirstNumber(int firstNumber)
    {
        mFirstNumber = firstNumber;
    }

    public void InputSymbol(string symbol)
    {
        mSymbol = symbol;
    }

    public void InputSecondNumber(int secondNumber)
    {
        mSecondNumber = secondNumber;
        Calculate(mFirstNumber, mSymbol, mSecondNumber);
    }

    void Calculate(int firstNumber, string symbol, int secondNumber)
    {
        if (symbol == "+")
        {
            Result = firstNumber + secondNumber;
            Debug.Log(Result);
        }
        else if (symbol == "-")
        {
            Result = firstNumber - secondNumber;
            Debug.Log(Result);
        }
        else if (symbol == "*")
        {
            Result = firstNumber * secondNumber;
            Debug.Log(Result);
        }
        else if (symbol == "/")
        {
            if (secondNumber == 0)
            {
                Debug.Log("警告:除法运算时,第二个数字不能为0!");
            }
            else
            {
                Result = firstNumber / secondNumber;
                Debug.Log(Result);
            }
        }
        else
        {
            Debug.Log("警告:请输入正确的运算符符号,+、-、*、/");
        }
    }

    static void Test(bool expression)
    {
        if (expression)
        {
            Debug.Log("OK");
        }
        else
        {
            Debug.LogError("Error");
        }
    }

    [MenuItem("计算器/Go")]
    static void Go()
    {
        Calculator aCalculator = new Calculator();
        aCalculator.InputFirstNumber(10);
        aCalculator.InputSymbol("/");
        aCalculator.InputSecondNumber(5);
        Test(aCalculator.Result == 2);
    }

    [MenuItem("计算器/打开界面")]
    static void OpenPanel()
    {
        Calculator calculator = (Calculator) GetWindow(typeof(Calculator), true);
        calculator.position = new Rect(0, 0, 800,600);
        calculator.Show();
    }
}